package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.Goods;
import com.jxc.model.po.Orderlist;
import com.jxc.model.po.OrderlistItem;
import com.jxc.model.po.Repertory;
import com.jxc.model.po.User;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.VOrderlist;
import com.jxc.model.vo.VOrderlistItem;
import com.jxc.service.OrderServiceI;

@Service("orderService")
public class OrderServiceImpl implements OrderServiceI {
	@Autowired
	private BaseDaoI<Orderlist> orderDao;
	@Autowired
	private BaseDaoI<OrderlistItem> itemDao;
	@Autowired
	private BaseDaoI<Goods> goodsDao;

	@Override
	public DataGrid datagrid(VOrderlist vOrderlist) {
		DataGrid dg = new DataGrid();
		dg.setRows(this.changeModel(this.find(vOrderlist)));
		dg.setTotal(this.total(vOrderlist));
		return dg;
	}

	private List<VOrderlist> changeModel(List<Orderlist> orderlists) {
		List<VOrderlist> vOrderlists = new ArrayList<VOrderlist>();
		if (orderlists != null && orderlists.size() > 0) {
			for (Orderlist orderlist : orderlists) {
				VOrderlist vOrderlist = new VOrderlist();
				BeanUtils.copyProperties(orderlist, vOrderlist);
				if (orderlist.getRepertory() != null) {
					Repertory repertory = orderlist.getRepertory();
					vOrderlist.setRid(repertory.getId());
					vOrderlist.setRname(repertory.getName());
				}
				if (orderlist.getUser() != null) {
					User user = orderlist.getUser();
					vOrderlist.setUid(user.getId());
					vOrderlist.setUname(user.getUsername());
				}
				vOrderlists.add(vOrderlist);
			}
		}
		return vOrderlists;
	}

	private List<Orderlist> find(VOrderlist vOrderlist) {
		String hql = "from Orderlist t where t.isDeleted = false ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vOrderlist, hql, values);

		if (vOrderlist.getSort() != null && vOrderlist.getOrder() != null) {
			hql += " order by " + vOrderlist.getSort() + " "
					+ vOrderlist.getOrder();
		}
		return orderDao.find(hql, values, vOrderlist.getPage(), vOrderlist
				.getRows());
	}

	private Long total(VOrderlist vOrderlist) {
		String hql = "select count(*) from Orderlist t where t.isDeleted=false ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vOrderlist, hql, values);
		return orderDao.count(hql, values);
	}

	private String addWhere(VOrderlist vOrderlist, String hql,
			List<Object> values) {
		if(vOrderlist.getId()!=null){
			hql += " and t.id = ? ";
			values.add(vOrderlist.getId());
		}
		if (vOrderlist.getDateStart() != null) {
			hql += " and t.date >= ? ";
			values.add(vOrderlist.getDateStart());
		}
		if (vOrderlist.getDateEnd() != null) {
			hql += " and t.date <= ? ";
			values.add(vOrderlist.getDateEnd());
		}
		if (vOrderlist.getRid() != null) {
			hql += " and t.repertory.id  = ? ";
			values.add(vOrderlist.getRid());
		}else{
			hql += " and t.repertory.id is null ";
		}
		return hql;
	}

	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					Orderlist orderlist = orderDao.get(Orderlist.class, Long
							.parseLong(id.trim()));
					if (orderlist != null) {
						orderlist.setIsDeleted(true);
						orderDao.update(orderlist);
					}
				}
			}
		}

	}

	@Override
	public void save(VOrderlist vOrderlist,String items) {
		Orderlist orderlist = new Orderlist();
		BeanUtils.copyProperties(vOrderlist, orderlist);
		if (vOrderlist.getRid() != null) {
			orderlist.setRepertory(new Repertory(vOrderlist.getRid()));
		}
		if (vOrderlist.getUid() != null) {
			orderlist.setUser(new User(vOrderlist.getUid()));
		}
		orderlist.setIsDeleted(false);
		orderlist.setDate(new Date());
		this.orderDao.save(orderlist);
		String[] objs = items.split(",");
		Double total=0d;
		if(objs.length>0){
			for(int i=0;i<objs.length;){
				Goods goods = goodsDao.get(Goods.class, Long.parseLong(objs[i]));
				Integer num = Integer.parseInt(objs[i+1]);
				if(goods!=null&&num>0){
					OrderlistItem item = new OrderlistItem();
					item.setBuyPrice(goods.getBuyPrice());
					item.setGoods(goods);
					item.setNum(num);
					item.setOrderlist(orderlist);
					item.setTotal(num*goods.getBuyPrice());
					itemDao.save(item);
					total+=item.getTotal();
					if (goods.getAmount()==null) {
						goods.setAmount(num);
					}else{
						goods.setAmount(goods.getAmount()+num);
					}
					goods.setOrderTotal(goods.getOrderTotal()+num);
					goodsDao.update(goods);
				}
				i=i+2;
			}
		}
		orderlist.setTotal(total);
		orderDao.update(orderlist);
	}

	@Override
	public List<VOrderlist> search(VOrderlist vOrderlist) {
		return changeModel(find(vOrderlist));
	}

	@Override
	public List<VOrderlistItem> getItems(VOrderlist vOrderlist) {
		String hql = "from OrderlistItem t where t.orderlist.id = ? ";
		List<Object> values = new ArrayList<Object>();
		values.add(vOrderlist.getId());
		List<OrderlistItem> items = itemDao.find(hql, values);
		return changeModelForItem(items);
	}
	
	
	private List<VOrderlistItem> changeModelForItem(List<OrderlistItem> items){
		List<VOrderlistItem> results = new ArrayList<VOrderlistItem>();
		if (items != null && items.size() > 0) {
			for (OrderlistItem item : items) {
				VOrderlistItem vOrderlistItem = new VOrderlistItem();
				BeanUtils.copyProperties(item, vOrderlistItem);
				if (item.getGoods() != null) {
					Goods goods = item.getGoods();
					vOrderlistItem.setGid(goods.getId());
					vOrderlistItem.setGname(goods.getName());
					vOrderlistItem.setBname(goods.getBrand().getName());
					vOrderlistItem.setColor(goods.getColor());
					vOrderlistItem.setGtname(goods.getGoodsType().getName());
					vOrderlistItem.setSize(goods.getSize());
					vOrderlistItem.setUnit(goods.getUnit());
					vOrderlistItem.setGtype(goods.getType());
				}
				results.add(vOrderlistItem);
			}
		}
		return results;
	}

	
}
